package post;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;


public class ProductCatalog {
    private ArrayList<ProductSpecification> ps = new ArrayList<ProductSpecification>();
    
    ProductCatalog() throws FileNotFoundException, IOException {
        BufferedReader file = new BufferedReader(new FileReader("products.txt"));
        ProductSpecification pss;
        String line = file.readLine();
        
        
        while(line != null) {
            String upc = line.substring(0, 4);
            String textDescription = line.substring(9, 29);
            String priceString = line.substring(33);
            Float price = Float.parseFloat(priceString);
            pss = new ProductSpecification(upc, textDescription, price);
            ps.add(pss);
            line = file.readLine();
        }
    }
    
    Item getItemAtIndex(int index) {
        ProductSpecification pss = this.ps.get(index);
        Item i = new Item(pss.getUpc(), pss.getTd(), pss.getPrice());
        
        return i;
    }
    
    Item getItemByUpc(String upc) {
        for(int i = 0; i < ps.size(); i++) {
            if(ps.get(i).getUpc().equals(upc)) {
                return new Item(ps.get(i).getUpc(), ps.get(i).getTd(), ps.get(i).getPrice());
            }
        }
        
        return new Item(null, null, null);
    }
}
